<?php

namespace app\common\cachemodel\wecaht;

use app\common\model\BaseCache;
use think\Log;
use XznsTools\cache\CacheManage;

class WechatUser extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.wechat_user';

    protected $cacheModel = 'wechat_h5.user';

    /**
     * 更新用户微信信息
     * @param int $id
     * @param array $data
     * @return bool
     * @throws \Exception
     */
    public function UpdateData(int $id, array $data)
    {
        try
        {
            $this->where(['id' => $id])->update($data);
        }
        catch (\Exception $e)
        {
            Log::write('app\common\cachemodel\wecaht\WechatUser 微信公众号 - 更新用户微信信息失败：' . $e->getMessage(), 'error', true);
            return false;
        }
        CacheManage::getInstance()->setCacheConfig($this->cacheModel, $id)->rmCache();
        return true;
    }

    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $data = $this->where(['id' => ['in', $ids]])->column('*', 'id');
        }
        catch (\Exception $e)
        {
            Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'sex', 'create_time', 'last_login_time', 'token_expires']);
        }

        return $data;
    }

}